package java08;
//定义SaleThread2类实现Runnable接口
class SaleThread2 implements Runnable{
    private int ticlets = 10;//10张票
    Object lock = new Object();//定义任意一个对象，用作同步代码块的锁
    public void run(){
        while(true){
            try {
                Thread.sleep(100);//模拟售票耗时过程
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock){//定义同步代码块
                if(ticlets > 0){
                    System.out.println(Thread.currentThread().getName() + "正在发售第" + ticlets-- +"张票");
                }
            }
        }
    }
}
public class LockTest01 {
    public static void main(String[] args){
        SaleThread2 saleThread = new SaleThread2();
        //创建4个线程，模拟4个售票口
        new Thread(saleThread,"窗口 1").start();
        new Thread(saleThread,"窗口 2").start();
        new Thread(saleThread,"窗口 3").start();
        new Thread(saleThread,"窗口 4").start();
    }
}
